package cn.leida.ruoyi.common.utils.poi;

import cn.hutool.core.util.ReflectUtil;
import cn.hutool.core.util.StrUtil;
import cn.leida.ruoyi.common.annotation.Excel;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author lei
 * @create 2025-05-02 1:00
 */
public class ExcelAnnotationUtil {

    public static Map<String, String> excelNameMap(Class<?> clazz) {
        Map<String, String> beanAliasMap = new LinkedHashMap<>();
        Field[] fields = ReflectUtil.getFields(clazz,
            field -> field.isAnnotationPresent(Excel.class));
        if (fields != null) {
            for (Field field : fields) {
                if (Modifier.isStatic(field.getModifiers())) {
                    continue;
                }
                Excel excel = field.getAnnotation(Excel.class);
                if (excel != null && StrUtil.isNotBlank(excel.name())) {
                    beanAliasMap.put(field.getName(), excel.name().trim());
                }
            }
        }
        return beanAliasMap;
    }
}
